package com.codeplay.controller;

import com.codeplay.entity.Course;
import com.codeplay.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
@CrossOrigin(origins = "*")
public class CourseController {

    private final CourseService courseService;

    @Autowired
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody Course course) {
        return ResponseEntity.ok(courseService.createCourse(course));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable Long id) {
        return ResponseEntity.ok(courseService.getCourseById(id));
    }

    @GetMapping
    public ResponseEntity<Page<Course>> getAllCourses(Pageable pageable) {
        return ResponseEntity.ok(courseService.getAllCourses(pageable));
    }

    @GetMapping("/difficulty/{level}")
    public ResponseEntity<Page<Course>> getCoursesByDifficultyLevel(
            @PathVariable Course.DifficultyLevel level, Pageable pageable) {
        return ResponseEntity.ok(courseService.getCoursesByDifficultyLevel(level, pageable));
    }

    @GetMapping("/age-range/{range}")
    public ResponseEntity<Page<Course>> getCoursesByAgeRange(
            @PathVariable String range, Pageable pageable) {
        return ResponseEntity.ok(courseService.getCoursesByAgeRange(range, pageable));
    }

    @GetMapping("/creator/{creatorId}")
    public ResponseEntity<List<Course>> getCoursesByCreator(@PathVariable Long creatorId) {
        return ResponseEntity.ok(courseService.getCoursesByCreator(creatorId));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<Course>> searchCourses(
            @RequestParam String keyword, Pageable pageable) {
        return ResponseEntity.ok(courseService.searchCourses(keyword, pageable));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Long id, @RequestBody Course course) {
        course.setId(id);
        return ResponseEntity.ok(courseService.updateCourse(course));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
        return ResponseEntity.ok().build();
    }
} 